<?php

namespace App\Http\Controllers;

use App\Activity;
use App\Business;
use App\Map;
use App\Type;
use App\User;
use Illuminate\Http\Request;

class BusinessController extends Controller {
	use Search;

	protected $model, $user;

	public function __construct(Business $business, User $user) {
		$this->model = $business;
		$this->user = $user;
	}

	public function index(Request $request) {
		return view('business.index');
	}

	public function activitys($id) {
		$business = $this->model->findOrFail($id)->load('activitys');
		return $business;
	}

	public function list(Request $request) {
		$business = $this->getBusiness();
		return view('business.list', compact('business'));
	}

	public function store(Request $request) {
		$this->validate($request, [
			'name' => 'required',
			'activity_id' => 'required',
			'type_id' => 'required',
			'type' => 'required',
			'action' => 'required',
		]);
		$id = $request->id ?: 0;
		$map_model = new Map();
		if ($request->action == 'create') {
			if ($request->type == 'business') {
				$this->validate($request, [
					'name' => 'required|unique:business',
				]);
				$business = $this->model->create(['name' => $request->name]);
				$map_model->create(['business_id' => $business->id, 'activity_id' => $request->activity_id, 'type_id' => $request->type_id]);
			} else {
				if (!$id) {
					return ['code' => 1, 'msg' => '参数错误'];
				}
				$map = $map_model->findOrFail($id);
				if ($this->checkExist($map, $request)) {
					return ['code' => 1, 'msg' => '该路径已存在'];
				}
				$new_map = $request->type == 'activity' ? ['business_id' => $map->business_id, 'activity_id' => $request->activity_id, 'type_id' => $request->type_id] : ['business_id' => $map->business_id, 'activity_id' => $map->activity_id, 'type_id' => $request->type_id];
				$map_model->create($new_map);
			}
		} else {
			if (!$id) {
				return ['code' => 1, 'msg' => '参数错误'];
			}
			$map = $map_model->findOrFail($id);
			if ($request->type == 'business') {
				$business = $this->model->where('name', $request->name)->first();
				if ($business && $business->id != $map->business_id) {
					return ['code' => 1, 'msg' => '该商家名已存在'];
				}
				if ($this->checkExist($map, $request)) {
					return ['code' => 1, 'msg' => '该路径已存在'];
				}
				$old_business = $this->model->findOrFail($map->business_id);
				$old_business->name = $request->name;
				$old_business->save();
				$map->activity_id = $request->activity_id;
				$map->type_id = $request->type_id;
				$map->save();
			} else {
				if ($this->checkExist($map, $request)) {
					return ['code' => 1, 'msg' => '该路径已存在'];
				}
				if ($request->type == 'activity') {
					$map->activity_id = $request->activity_id;
					$map->type_id = $request->type_id;
				} else {
					$map->type_id = $request->type_id;
				}
				$map->save();
			}
		}
		return json_encode(['code' => 200, 'msg' => 'success']);
	}

	public function create($type, Request $request) {
		$types = (new Type)->get();
		$activitys = (new Activity)->get();
		$id = $request->input("id");
		if ($id) {
			$map = (new Map)->findOrFail($id);
			$map->load(['business', 'activity', 'type']);
		}
		$action = 'create';
		return view('business.form', compact('types', 'activitys', 'map', 'type', 'action'));
	}

	public function edit(Map $map, $type) {
		if (count($map->orders) > 0) {
			$msg = "该路径下已有导入订单，不可编辑";
			return view('alert', compact('msg'));
		}

		$types = (new Type)->get();
		$activitys = (new Activity)->get();
		$map->load(['business', 'activity', 'type']);
		$action = 'edit';
		return view('business.form', compact('types', 'activitys', 'map', 'type', 'action'));
	}

	public function destroy(Map $map) {
		if (count($map->orders) > 0) {
			$msg = "该路径下已有导入订单，不可删除";
			return view('alert', compact('msg'));
		}
		$map->delete();
		$msg = "删除成功";
		$existBusiness = (new Map)->where('business_id', $map->business_id)->get();
		if (!count($existBusiness) > 0) {
			$this->model->destroy($map->business_id);
		}
		return view('alert', compact('msg'));
	}

	private function checkExist(Map $map, Request $request, $type = "create") {
		$old_map = (new Map)->where('business_id', $map->business_id)->where('activity_id', $request->activity_id)->where('type_id', $request->type_id)->first();
		return $old_map ? ($type == "create" ? true : ($old_map->id == $map->id ? false : true)) : false;
	}
}
